package com.retail.proxy.controller;


import cn.hutool.core.util.ObjectUtil;
import com.retail.core.common.AbstractCommonController;
import com.retail.core.entity.ParamReq;

import com.retail.core.entity.result.PageResult;
import com.retail.core.entity.result.Result;
import com.retail.global.security.LoginUser;
import com.retail.global.security.LoginUserUtil;
import com.retail.global.annon.TraceLog;
import com.retail.proxy.entity.request.SysAdminApplyProxyRequest;
import com.retail.proxy.entity.vo.SysAdminApplyProxyVo;
import com.retail.proxy.service.SysAdminApplyProxyService;
import com.retail.system.entity.request.AdminRequest;
import com.retail.system.enums.UserTypeEnums;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* 申请代理 API
* @author Administrator
*/
@Api(tags = "申请代理 API")
@RestController
@RequestMapping("/sysAdminApplyProxy")
public class SysAdminApplyProxyController extends AbstractCommonController<SysAdminApplyProxyService, SysAdminApplyProxyRequest, SysAdminApplyProxyVo> {

    @Autowired
    private SysAdminApplyProxyService sysAdminApplyProxyService;

    @TraceLog()
    @GetMapping("{id}")
    public Result getModel(@PathVariable("id") Long id) {
        return super.getModel(sysAdminApplyProxyService, id);
    }

    @TraceLog()
    @PostMapping
    @ApiOperation("保存申请代理")
    public Result<Integer> save(@RequestBody SysAdminApplyProxyRequest request) {
        request.setCreateUser(LoginUserUtil.getUserId());
        return super.save(sysAdminApplyProxyService, request);
    }

    @TraceLog()
    @PutMapping
    @ApiOperation("更新申请代理")
    public Result<Integer> update(@RequestBody SysAdminApplyProxyRequest request) {
        request.setCreateUser(LoginUserUtil.getUserId());
        return super.update(sysAdminApplyProxyService, request);
    }

    @TraceLog()
    @PostMapping("listPage")
    @ApiOperation("申请代理-查询")
    public Result<List<SysAdminApplyProxyVo>>listPage(@RequestBody ParamReq req) {
        return super.listPage(sysAdminApplyProxyService, req);
    }

    @TraceLog()
    @PostMapping("countPage")
    @ApiOperation("申请代理-统计")
    public Result<Integer> countPage(@RequestBody ParamReq req) {
        return super.countPage(sysAdminApplyProxyService, req);
    }

    @TraceLog()
    @PostMapping("page")
    @ApiOperation("申请代理-分页数据")
    public Result<PageResult> page(@RequestBody ParamReq<SysAdminApplyProxyRequest> req) {
        SysAdminApplyProxyRequest adminRequest = req.getEntity();
        Long adminId = getLoginUserId();
        LoginUser loginUser = LoginUserUtil.getPrincipal();
        if (ObjectUtil.equal(adminRequest.getUserType(), UserTypeEnums.BUSINESS_TYPE.getValue())) {
            if (loginUser.getUserType().equals(UserTypeEnums.SYSTEM_MANAGER_TYPE.getValue())) {
                adminRequest.setSearchOwnerUser(2);
            } else {
                adminRequest.setSearchOwnerUser(1);
                adminRequest.setSearchOwnerUserId(adminId);
            }
        }



        return super.page(sysAdminApplyProxyService, req);
    }

    @TraceLog()
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除申请代理")
    public Result<Integer> delete(@PathVariable("id") Long id) {
        return super.delete(sysAdminApplyProxyService, id);
    }

    @TraceLog()
    @ApiOperation("批量删除申请代理")
    @DeleteMapping("{ids}")
    public Result<Integer> batchDelete(List<Long> ids) {
        return super.batchDelete(sysAdminApplyProxyService, ids);
    }

    @Override
    public Long getLoginUserId() {
        return LoginUserUtil.getUserId();
    }

}
